import react from "@vitejs/plugin-react-swc";
import { defineConfig } from "vite-common";
import { viteTinyfy } from "vite-plugin-tinyfy";

const BUILD_ENV = process.env.BUILD_ENV;
const APP_MSW_MOCK = process.env.APP_MSW_MOCK;

// https://vitejs.dev/config/
export default defineConfig({
  server: {
    open: true,
    port: 4000,
    proxy: {
      "/pro-api": {
        target: `https://${BUILD_ENV}-mobile-api.bankneo.co.id`,
        rewrite: (path: string) => path.replace(/^\/pro-api/, ""),
        changeOrigin: true,
        secure: false,
      },
      "/api": {
        target: `http://localhost:3000/`,
        rewrite: (path: string) => path.replace(/^\/api/, ""),
        changeOrigin: true,
        secure: false,
      },
    },
  },
  plugins: [react(), viteTinyfy()],
  define: {
    BUILD_ENV: JSON.stringify(BUILD_ENV),
    APP_MSW_MOCK: JSON.stringify(APP_MSW_MOCK),
  },
  build: {
    emptyOutDir: true,
    cssTarget: ["chrome53"],
    rollupOptions: {
      output: {
        manualChunks: {
          react: ["react"],
          reactDom: ["react-dom"],
          reactRouterDom: ["react-router-dom"],
          reactRedux: ["react-redux"],
          reduxjs: ["@reduxjs/toolkit"],
          axios: ["axios"],
        },
      },
    },
  },
  optimizeDeps: {
    include: ["react", "react-dom", "react-router-dom", "react-redux", "@reduxjs/toolkit", "axios"],
  },
});
